package com.yunbao.hujin.myguidepage;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/12/1.
 */

public class BaseActivity extends AppCompatActivity{
    public static final String SP_FILE = "MyGuide";      //偏好设置保存文件名

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_ACTION_BAR);        //隐藏标题栏-FEATURE_NO_TITLE
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;   //定义全屏参数
        Window window = this.getWindow();                       //获得当前窗体对象
        window.setFlags(flag, flag);                             //设置当前窗体为全屏显示
//        setContentView(getLayoutId());
    }

    /**
     * 判断是否是第一次进入app
     */
    public boolean isFirst() {
        SharedPreferences preferences = this.getSharedPreferences(SP_FILE, Context.MODE_PRIVATE);
        boolean isFirst = preferences.getBoolean("isFirst", true);
        return isFirst;
    }


    public void saveFirst() {
        SharedPreferences preferences = this.getSharedPreferences(SP_FILE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("isFirst", false);
        editor.apply();
    }



//    protected abstract int getLayoutId();


    /**
     * 双击退出程序
     */
    private Boolean isExit = false;
    private void setIsExit() {
        if (!isExit) {
            isExit = true;                   // 准备退出
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false;         // 取消退出
                }
            }, 2000);       // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

        } else {
            this.overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
            finish();
            System.exit(0);
        }
    }

    //此方法兼容Android 1.0到Android 2.1 也是常规方法
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        } else {
            return false;
        }
    }

    //该方法仅适用于2.0或更新版的sdk
    @Override
    public void onBackPressed() {
        setIsExit();
    }


}
